package com.ychug.gateway.system;


import com.ychug.common.base.ResultAjaxHash;
import com.ychug.framework.web.base.BaseController;
import com.ychug.framework.web.page.TableDataInfo;
import com.ychug.service.system.entity.SysDictDataEntity;
import com.ychug.service.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/sys/dict/data")
public class SysDictDataController extends BaseController
{
    private String prefix = "modules/sys/dict/data";

    @Autowired
    private ISysDictDataService dictDataService;


    @GetMapping()
    public String dictData()
    {
        return prefix + "/viewData";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDictDataEntity dictData)
    {
        startPage();
        List<SysDictDataEntity> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }


    @PostMapping("/export")
    @ResponseBody
    public ResultAjaxHash export(SysDictDataEntity dictData)
    {
//        List<SysDictDataEntity> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictDataEntity> utils = new ExcelUtil<SysDictData>(SysDictData.class);
//        return utils.exportExcel(list, "dictData");
        return  null;
    }

    /**
     * 新增字典类型
     */
    @GetMapping("/add/{dictType}")
    public String add(@PathVariable("dictType") String dictType, ModelMap mmap)
    {
        mmap.put("dictType", dictType);
        return prefix + "/add";
    }

    /**
     * 新增保存字典类型
     */

    @PostMapping("/add")
    @ResponseBody
    public ResultAjaxHash addSave(SysDictDataEntity dict)
    {
        dict.setCreateBy("admin");
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{dictCode}")
    public String edit(@PathVariable("dictCode") Long dictCode, ModelMap mmap)
    {
        mmap.put("dict", dictDataService.selectDictDataById(dictCode));
        return prefix + "/edit";
    }

    /**
     * 修改保存字典类型
     */

    @PostMapping("/edit")
    @ResponseBody
    public ResultAjaxHash editSave(SysDictDataEntity dict)
    {
        dict.setUpdateBy("admin");
        return toAjax(dictDataService.updateDictData(dict));
    }


    @PostMapping("/remove")
    @ResponseBody
    public ResultAjaxHash remove(String ids)
    {
        return toAjax(dictDataService.deleteDictDataByIds(ids));
    }
}
